<?php
$loader->requireOnce('includes/inPatientCore/InPatientCoreErrorCode.class.php');
$loader->requireOnce('includes/inPatientCore/InPatientCoreException.class.php');
$loader->requireOnce('includes/inPatientCore/InPatientSEBase.class.php');
$loader->requireOnce('ordo/InPatient.class.php');
$loader->requireOnce('ordo/EmployeeType.class.php');
$loader->requireOnce('ordo/Service.class.php');



/**
 * InPatientSurgery Processor.
 * @package core.includes.inPatientCore
 * @author viethd
 * @version $Id$
 * @copyright 2009 LHV Company
 */
class InPatientSurgery extends InPatientSEBase{
	private $logger ;


	/**
	 *
	 * @param $controller controller for build ui
	 */
	function __construct($controller) {
		parent::__construct($controller);
		$this->logger = LoggerManager::getLogger('core.inPatientCore.InPatientSurgery');
		

	}

	public function loadInputForm($ticketId) {
	    $recordId     =   $_GET['recordId'];
	    
        $surPart    =   ORDataObject::factory('SurgeryParticipant');
        $roleList   =   $surPart->getParticipantRoleList();                 
              
		$path = $this->controller->view->path;
		$employeeObj = Celini::newORDO("Employee");
		$doctorList = $employeeObj->getEmployeeOfDepartment($this->controller->_me->get_department_id(),
																		EmployeeType::DOCTOR_ID, true);
		$emr = Celini::newORDO("InPatientEmr");
        $emrInfo =  $emr->getEmrInfo($ticketId);

		$surgeryObj = Celini::newORDO("Surgery");
		$classify = Utils::getEnumKey(EnumerationConstant::SURGERY_CLASSIFY, 
									EnumerationConstant::SURGERY_CLASSIFY_PHAUTHUAT);
        $surgeriesEmrDetail = $surgeryObj->getSurgeryEmrDetailInfo($this->controller->_me->get_department_id(), 
       																		$emrInfo['in_patient_emr_id']);
		$tbody = $this->buildSurgeriesEmrDetailTable($ticketId, $surgeriesEmrDetail);
	    $this->controller->view->path = "inpatient_surgery";
        $this->controller->assign("participantRoles",$roleList);
        $this->controller->assign('ticketId', $ticketId);
        $this->controller->assign('doctorList', $doctorList);
        $this->controller->assign('tbody', $tbody);
        $form = $this->controller->view->render('input_surgery.html');
        $this->controller->view->path = $path;
        return $form;
	}
	
	public function deleteInPatientSurgery($ticketId, $surgeryId) {

		$participant = Celini::newOrdo('SurgeryParticipant');
        $participant->deleteParticipants($surgeryId);

        $surgeryObj = Celini::newOrdo('Surgery');
        $surgeryObj->deleteSurgery($surgeryId);
        $itemType = Utils::getEnumKey(EnumerationConstant::IN_TICKE_ITEM_ITEMTYPE,
        EnumerationConstant::IN_TICKE_ITEM_ITEMTYPE_SURGERY);
        $inTicketItemObj = Celini::newOrdo('InTicketItem');
        $inTicketItemObj->deleteItem($surgeryId, $itemType);


	}
	
    public function buildInPatientSurgeryTeam($ticketId, $surgeryId) {

        $surPart    =   ORDataObject::factory('SurgeryParticipant');
        $roleList   =   $surPart->getParticipantRoleList();
        $pSur = $surPart->getParticipantsDetailInfoBySurgeryId($surgeryId);
        $path = $this->controller->view->path;
        $this->controller->view->path = "inpatient_surgery";
        $this->controller->assign("participants",$pSur);
        $this->controller->assign('ticketId', $ticketId);
        $this->controller->assign("participantRoles",$roleList);
        $form = $this->controller->view->render('surgery_team.html');
        $this->controller->view->path = $path;
        return $form;
    }
    
    public function buildInPatientSurgeryTeamTemplate() {
        //  Load Surgery Team Template
        $obj = Celini::newORDO('SurgeryTeamTemplate');
        $templateId = SurgeryTeamTemplate::NORMAL_TEMPLATE;     
        $templates = $obj->listCommonTemplates($templateId);
        
        $path = $this->controller->view->path;
        $this->controller->view->path = "inpatient_surgery";
        
        
        $this->controller->assign('templates', $templates);
        if (sizeof($templates) > 0) {
            $this->controller->assign('firstTemplateId', $templates[0]['template_id']);
        }

        $form = $this->controller->view->render('surgery_team_template.html');
        $this->controller->view->path = $path;
        return $form;
    }

	private function buildSurgeriesEmrDetailTable($ticketId, $surgeriesEmrDetail) {
		$html = "";
		foreach ($surgeriesEmrDetail as $row) {
			$html .= "<tr id = 'surgery". $row['surgery_id'] . "'>";
			$html .= "<td>";
			$html .= $row['date'];
			$html .= "</td>";

			$html .= "<td>";
			$html .= $row['name'];
            $html .= "</td>";

            $html .= "<td>";
            $html .= L10nHelper::renderNumber($row['original_price'],0);
            $html .= "</td>";

            $html .= "<td>";
            $html .= L10nHelper::renderNumber($row['insurance_price'],0);
            $html .= "</td>";

            $html .= "<td>";
            $html .= L10nHelper::renderNumber($row['patient_price'],0);
            $html .= "</td>";

            $html .= "<td>";
            $html .= "<a href=\"javascript:displaySurgeryTeam('". $ticketId ."', '" . $row['surgery_id'] .  "')\">" . L10nHelper::_("Team") . "</a>";
            $html .= "</td>";

            $html .= "<td>";
            $html .= "<a href=\"javascript:deleteInPatientSurgery('$ticketId','" . $row['surgery_id'] . "')\"> <img src='../images/remove_small.png' > </a>";
            $html .= "</td>";

            $html .= "</tr>";
		}
		return $html;

	}

	public function addSurgeryService($ticketId, $surgeryServiceId, $surgeryDate, $doctorId) {
		$emr = Celini::newORDO("InPatientEmr");
        $emrInfo =  $emr->getEmrInfo($ticketId);

		$surgeryObj = Celini::newOrdo('Surgery');
		$surgeryObj->setData($emrInfo['in_patient_emr_id'], $surgeryDate, '', '',
        '', '', $surgeryServiceId, '');
		$surgeryObj->persist();
		$surgeryId = $surgeryObj->get_id();
		$serviceObj = Celini::newOrdo('Service');
		$prices = $serviceObj->getPrices($ticketId, $surgeryServiceId, EnumerationConstant::SER_INS_ITEM_TYPE_PTTT);

		$participant = Celini::newOrdo('SurgeryParticipant');
		$participant->setData($surgeryId, $doctorId, EmployeeType::DOCTOR_ID);
		$participant->persist();
		$itemType = Utils::getEnumKey(EnumerationConstant::IN_TICKE_ITEM_ITEMTYPE,
		EnumerationConstant::IN_TICKE_ITEM_ITEMTYPE_SURGERY);
		$inTicketItemObj = Celini::newOrdo('InTicketItem');
		$inTicketItemObj->setData($surgeryId,
        $itemType,
        0,$prices['price'],$prices['insurance_price'], $prices['patient_price'], $ticketId, null);
        $inTicketItemObj->persist();

	}

	
}
?>